<template>
  <div>
    <Category :scene="scene" />
    <el-card style="margin: 10px 0px">
      <!-- scene === 0 展示已有的SPU数据 -->
      <div v-show="scene === 0">
        <el-button @click="addSpu" type="primary" size="default" icon="Plus">
          添加SPU
        </el-button>
        <el-table style="margin: 10px 0px" border :data="records">
          <el-table-column
            label="序号"
            type="index"
            align="center"
            width="80px"
          />
          <el-table-column label="SPU名称" prop="spuName"></el-table-column>
          <el-table-column
            label="SPU描述"
            prop="description"
            show-overflow-tooltip
          />
          <el-table-column label="SPU操作">
            <!-- row:即为已有的SPU对象 -->
            <template #="{ row, $index }">
              <el-button
                type="primary"
                size="small"
                icon="Plus"
                title="添加SKU"
                @click="addSku(row)"
              />
              <el-button
                type="primary"
                size="small"
                icon="Edit"
                title="添加SKU"
                @click="updateSpu(row)"
              />
              <el-button
                type="primary"
                size="small"
                icon="View"
                title="查看SKU列表"
                @click="findSku(row)"
              />
              <el-popconfirm
                :title="`你确定要删除${row.spuName}吗？`"
                width="200px"
                @confirm="deleteSpu(row)"
              >
                >
                <template #reference>
                  <el-button
                    type="primary"
                    size="small"
                    icon="Delete"
                    title="删除SPU"
                  />
                </template>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination
          v-model:current-page="pageNo"
          v-model:page-size="pageSize"
          :page-sizes="[3, 5, 7, 9]"
          :background="true"
          layout="prev, pager, next, jumper,->,sizes,total"
          :total="total"
          @current-change="getHasSpu"
          @size-change="changeSize"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref, watch } from 'vue'
import { Records, HasSpuResponseData } from '@/api/product/spu/type'
// 三级分类下SPU数据接口
import { reqHasSpu } from '@/api/product/spu/index'
// 引入分类的仓库
import useCategoryStore from '@/store/modules/category'
import { ca } from 'element-plus/es/locale'
let categoryStore = useCategoryStore()
// scene 控制三级分类下拉列表的选择
//0:显示已有SPU  1:添加或者修改已有SPU 2:添加SKU的结构
let scene = ref<number>(0)
// 分页
let pageNo = ref<number>(1)
let pageSize = ref<number>(3)
let total = ref<number>(0)
// table表格保定的数据源
const records = ref<Records>([])
//监听三级分类ID变化
watch(
  () => categoryStore.c3Id,
  () => {
    //当三级分类发生变化的时候清空对应的数据
    records.value = []
    //务必保证有三级分类ID
    if (!categoryStore.c3Id) return
    getHasSpu()
  },
)
//此方法执行:可以获取某一个三级分类下全部的已有的SPU
const getHasSpu = async (pager = 1) => {
  console.log('🚀 ~ getHasSpu ~ pager:', pager)
  //修改当前页码
  pageNo.value = pager
  let result: HasSpuResponseData = await reqHasSpu(
    pageNo.value,
    pageSize.value,
    categoryStore.c3Id,
  )
  if (result.code == 200) {
    records.value = result.data.records
    total.value = result.data.total
  }
}
//分页器下拉菜单发生变化的时候触发
const changeSize = () => {
  getHasSpu()
}
// table操作中按钮--添加SKU
const addSku = (row) => {}
// table操作中按钮--修改SPU
const updateSpu = (row) => {}
// table操作中按钮--查看SKU列表
const findSku = (row) => {}
// table操作中按钮--删除
const deleteSpu = (row) => {}

// 添加SPU按钮的回调
const addSpu = () => {
  // 切换为场景1：添加与修改已有SPU结构->SpuForm
  scene.value = 1
}
</script>

<style scoped></style>
